﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using RabbitMQ.Client.Events;

namespace Keede.RabbitMQ
{
    /// <summary>
    /// queue listener
    /// </summary>
    public class QueueListener : IQueueListener
    {
        /// <summary>
        /// queue name
        /// </summary>
        public string QueueName { get; set; } = "";

        /// <summary>
        /// consumer tag, default value is null
        /// </summary>
        public string ConsumerTag { get; set; } = "";


        /// <summary>
        /// arguments
        /// </summary>
        public IDictionary<string, object> Arguments { get; set; } = null;

        /// <summary>
        /// is auto ask
        /// </summary>
        public bool AutoAsk { get; private set; } = false;

        Func<string, BasicDeliverEventArgs, Task> _messageReceivedNoAskHandler;

        /// <summary>
        /// message received handler with auto ask
        /// string message
        /// BasicDeliverEventArgs: raw
        /// </summary>
        public Func<string, BasicDeliverEventArgs, Task> MessageReceivedAutoAskHandler
        {
            get => _messageReceivedNoAskHandler;
            set
            {
                if (_messageReceivedHandler != null) throw new Exception("One listener allows only one handler, another MessageReceivedHandler was not null");

                AutoAsk = true;
                _messageReceivedNoAskHandler = value;
            }
        }

        Func<string, BasicDeliverEventArgs, Task<bool>> _messageReceivedHandler;

        /// <summary>
        ///message received handler without auto ask
        /// 
        /// Paramters:
        ///[1] message content 
        ///[2] raw message information
        /// 
        ///Return:
        ///if need requeue return true,else return false
        /// </summary>
        public Func<string, BasicDeliverEventArgs, Task<bool>> MessageReceivedHandler
        {
            get => _messageReceivedHandler;
            set
            {
                if (_messageReceivedNoAskHandler != null) throw new Exception("One listener allows only one handler, another MessageReceivedNoAskHandler was not null");

                AutoAsk = false;
                _messageReceivedHandler = value;
            }
        }
    }
}
